﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;

namespace efuture.vdrp
{
    public partial class frmFormatConditionsEditor : DevExpress.XtraEditors.XtraForm
    {
        public frmFormatConditionsEditor()
        {
            InitializeComponent();
            InitConditions();
        }

        class ItemCondition
        {
            public static string ConditionsName = "(Format Conditions)";
            StyleFormatCondition fcondition;
            public ItemCondition(StyleFormatCondition fcondition)
            {
                this.fcondition = fcondition;
            }
            public override string ToString()
            {
                if (fcondition.Condition != FormatConditionEnum.None)
                    if (fcondition.Condition == FormatConditionEnum.Between || fcondition.Condition == FormatConditionEnum.NotBetween)
                    {
                        return string.Format("{0} {1}, {2}", fcondition.Condition, fcondition.Value1, fcondition.Value2);
                    }
                    else
                        return string.Format("{0} {1}", fcondition.Condition, fcondition.Value1);
                return string.Format("Condition Item - Index {0}", Index);
            }
            public int Index { get { return fcondition.Column.View.FormatConditions.IndexOf(fcondition); } }
            public StyleFormatCondition Condition { get { return fcondition; } }
        }

        GridColumn column1;
        public GridColumn GridColumn
        {
            get { return column1; }
            set { column1 = value; }
        }
        GridView GridView
        {
            get { return GridColumn.View as GridView; }
        }

        private void FillData()
        {
            FormatItemList.Items.Clear();
            if (GridColumn == null || GridView == null) return;
            for (int i = 0; i < GridView.FormatConditions.Count; i++)
            {
                if (GridColumn.Equals(GridView.FormatConditions[i].Column))
                    FormatItemList.Items.Add(new ItemCondition(GridView.FormatConditions[i]));
            }
            if (FormatItemList.Items.Count > 0) FormatItemList.SelectedIndex = 0;
            else InitItem(null);
        }

        void InitConditions()
        {
            Array values = System.Enum.GetValues(typeof(FormatConditionEnum));
            foreach (object obj in values)
            {
                if (obj.ToString().IndexOf("Expression") < 0)
                    icbCondition.Properties.Items.Add(new ImageComboBoxItem(obj.ToString(), obj, -1));
            }
        }

        private void ConditionsEditor_Load(object sender, EventArgs e)
        {
            FillData();
        }

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (init || item == null) return;
            item.Condition = (FormatConditionEnum)icbCondition.EditValue;
            RaiseRefreshValues();
        }

        void SetCheckEditStyle(BaseEdit be, string s)
        {
            CheckEdit ce = (CheckEdit)be;
            ce.Properties.AllowGrayed = true;
            ce.Text = s;
        }

        void SetSpinStyle(BaseEdit be, Type type)
        {
            SpinEdit se = (SpinEdit)be;
            if ("System.Int16System.Int32System.Int64System.Byte".IndexOf(type.ToString()) > -1)
                se.Properties.IsFloatValue = false;
        }

        BaseEdit be1 = null;
        BaseEdit be2 = null;
        StyleFormatCondition item = null;
        bool init = false;
        public event EventHandler RefreshValues;

        void CreateValues(Type type)
        {
            if (type.Equals(typeof(Boolean)))
            {
                be1 = new CheckEdit();
                be2 = new CheckEdit();
                SetCheckEditStyle(be1, "Value1");
                SetCheckEditStyle(be2, "Value2");
                lbVal1.Visible = lbVal2.Visible = false;
            }
            else if (type.Equals(typeof(DateTime)))
            {
                be1 = new DateEdit();
                be2 = new DateEdit();
            }
            else if (type.IsValueType)
            {
                be1 = new SpinEdit();
                be2 = new SpinEdit();
                SetSpinStyle(be1, type);
                SetSpinStyle(be2, type);
            }
            else
            {
                be1 = new TextEdit();
                be2 = new TextEdit();
            }
            be1.Size = be2.Size = new Size(icbCondition.Width, icbCondition.Height);
            if (type.Equals(typeof(Boolean)))
            {
                be1.Location = lbVal1.Location;
                be2.Location = lbVal2.Location;
            }
            else
            {
                be1.Location = new Point(icbCondition.Location.X, lbVal1.Location.Y);
                be2.Location = new Point(icbCondition.Location.X, lbVal2.Location.Y);
            }
            be1.Validated += new EventHandler(val1_Validate);
            be2.Validated += new EventHandler(val2_Validate);
            be1.TabIndex = 2;
            be2.TabIndex = 3;
            this.Controls.Add(be1);
            this.Controls.Add(be2);
        }

        void InitItem(StyleFormatCondition item)
        {
            this.item = item;
            init = true;
            bool IsNull = item == null;
            if (!IsNull)
            {
                if (be1 == null) CreateValues(item.Column.ColumnType);
                ceRow.Checked = item.ApplyToRow;
                propertyGrid1.SelectedObject = item.Appearance;
                icbCondition.EditValue = item.Condition;
                be1.EditValue = item.Value1;
                be2.EditValue = item.Value2;
            }
            ceRow.Enabled = icbCondition.Enabled = !IsNull;
            if (be1 != null) be1.Visible = be2.Visible = !IsNull;
            if (IsNull) propertyGrid1.SelectedObject = null;
            init = false;
        }

        void RaiseRefreshValues()
        {
            if (RefreshValues != null) RefreshValues(this, EventArgs.Empty);
        }

        void SetValue(bool value1, object val)
        {
            try
            {
                if (val != null)
                {
                    val = Convert.ChangeType(val, item.Column.ColumnType);
                }
                if (value1)
                    item.Value1 = val;
                else
                    item.Value2 = val;
            }
            catch
            {
            }
            init = true;
            if (value1) be1.EditValue = item.Value1;
            else be2.EditValue = item.Value2;
            init = false;
            RaiseRefreshValues();
        }

        void val1_Validate(object sender, EventArgs e)
        {
            if (init || item == null) return;
            SetValue(true, be1.EditValue);
        }

        void val2_Validate(object sender, EventArgs e)
        {
            if (init || item == null) return;
            SetValue(false, be2.EditValue);
        }

        private void ceRow_CheckedChanged(object sender, EventArgs e)
        {
            if (init || item == null) return;
            item.ApplyToRow = ceRow.Checked;
        }

        private void sbAdd_Click(object sender, EventArgs e)
        {
            if (GridView == null) return;
            FormatItemList.Items.BeginUpdate();
            StyleFormatCondition item = new StyleFormatCondition();
            item.Column = this.GridColumn;
            GridView.FormatConditions.Add(item);
            ItemCondition iCondition = new ItemCondition(item);
            FormatItemList.Items.Add(iCondition);
            FormatItemList.SelectedItem = iCondition;
            FormatItemList.Items.EndUpdate();
        }

        void SetRemoveEnabled()
        {
            sbRemove.Enabled = FormatItemList.SelectedIndex > -1;
        }

        private void FormatItemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetRemoveEnabled();
            if (FormatItemList.SelectedItem != null)
                InitItem(GridView.FormatConditions[((ItemCondition)FormatItemList.SelectedItem).Index]);
            else InitItem(null);
        }

        private void icbCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (init || item == null) return;
            item.Condition = (FormatConditionEnum)icbCondition.EditValue;
            RaiseRefreshValues();
        }

        private void sbRemove_Click(object sender, EventArgs e)
        {
            if (GridView == null) return;
            ItemCondition condition = FormatItemList.SelectedItem as ItemCondition;
            if (condition == null) return;
            GridView.FormatConditions.RemoveAt(condition.Index);
            FormatItemList.Items.Remove(condition);
        }

    }
}